﻿using System;

using System.Collections.Generic;
using System.Text;
using Silvermoon.Visuals;
using Silvermoon.OpenGL;
using System.IO;
using System.Drawing;
using Silvermoon.Core;
using Silvermoon.OpenGL.Native;

namespace Silvermoon.Visuals
{
    public class ImageVisual : Visual
    {
        private Texture texture;
        private short[] rectangleVertex;

        public Size TextureSize { get { return texture != null ? texture.Size : Size.Empty; } }
        public Color Color { get; set; }

        /// <summary>
        /// Gets or sets the TextureElement that holds the image.
        /// </summary>
        public Texture Image
        {
            get { return texture; }
            set
            {
                if (texture != value)
                {
                    texture = value;
                    Invalidate();
                }
            }
        }

        public bool UseTextColor { get; set; }


        protected override Color GetPrimitiveColor()
        {
            return Color.IsEmpty ? (UseTextColor ? Screen.Current.Renderer.TextColor : Color.Empty) : Color;
        }

        protected override void CreateElements()
        {
            Size r = Size;
            short right = (short)(r.Width);
            short bottom = (short)(r.Height);

            rectangleVertex = new short[] 
                    { 
                        right, bottom,
                        right, 0,
                        0, bottom,
                        0, 0 
                    };

            base.CreateElements();
        }


        public override void Render(Renderer renderer)
        {
            texture.Render();
            gl.VertexPointer(2, glValueType.Short, 0, rectangleVertex);
            gl.DrawArrays(PrimitiveType.TriangleStrip, 0, rectangleVertex.Length / 2);
        }
    }
}
